home *** CD-ROM | disk | FTP | other *** search
/ Aminet 30 / Aminet 30 (1999)(Schatztruhe)[!][Apr 1999].iso / Aminet / dev / cross / GBDK-2.0.lha / GBDK / doc / libs.sgml < prev    next >
SGML Document  |  1998-10-01  |  15KB

  1. <!DOCTYPE Book PUBLIC "-//Davenport//DTD DocBook V2.4.1//EN">
  2. <book>
  3. <title>GBDK libraries documentation</title>
  4. <bookinfo>
  5. <bookbiblio>
  6. <authorgroup>
  7. <author><firstname>Michael</firstname><surname>Hope</surname></author>
  8. </authorgroup>
  9. <productname>Libraries for GBDK</productname>
  10. <pubdate>July 3rd 1998</pubdate>
  11. <copyright>
  12. <year>1998</year>
  13. <holder>Michael Hope</holder>
  14. </copyright>
  15. <abstract>
  16. <para>
  17. This document describes the functions included in the libraries for Pascal Felber's
  18. GBDK that are Gameboy specific.  Currently included are sections on the malloc lib
  19. and the multiple font support routines.
  20. Unfortunately I'm still learning how to use SGML.  Any help would be appreciated :)
  21. </para>
  22. </abstract>
  23. </bookbiblio>
  24. </bookinfo>
  25. <toc></toc>
  26. <chapter id="malloc-lib">
  27. <title>sys/malloc.h - malloc, free and related functions</title>
  28. <sect1><title>Functions</title>
  29. <sect2 id="malloc"><title>malloc</title>
  30. <para>
  31. <synopsis>void *malloc( <link linkend="size-t">size_t</link> numbytes )</synopsis>
  32. Allocate numbytes of memory from the free memory pool and return 
  33. a pointer to the base of the newly allocated region.
  34. </para>
  35. <sect3><title>Description</title>
  36. <para>
  37. Note that the memory is not cleared upon allocation.
  38. </para></sect3>
  39. <sect3><title>Parameters</title>
  40. <para>
  41. numbytes:  The number of bytes to allocate
  42. </para></sect3>
  43. <sect3><title>Returns</title>
  44. <para>
  45. On success, returns a pointer to the newly allocated region.
  46. On failure returns <link linkend="null">NULL</link>.
  47. </para></sect3>
  48. </sect2>
  49.  
  50. <sect2 id="calloc"><title>calloc</title>
  51. <para>
  52. <synopsis>void *calloc( <link linkend="size-t">size_t</link> nmem, <link linkend="size-t">size_t</link> size )</synopsis>
  53. Attempt to allocate space for nmem objects of size size and return a pointer to the allocated region.
  54. </para>
  55. <sect3><title>Description</title>
  56. <para>
  57. calloc is very similar to malloc but it also clears (fills with zero) the memory region
  58. before returning.
  59. </para></sect3>
  60. <sect3><title>Parameters</title>
  61. <para>
  62. size: size of one object
  63. </para>
  64. <para>
  65. nmem:  Number of objects to allocate space for
  66. </para></sect3>
  67. <sect3><title>Returns</title>
  68. <para>
  69. On success, returns a pointer to the newly allocated and cleared region.
  70. On failure, returns <link linkend="null">NULL</link>.
  71. </para></sect3>
  72. </sect2>
  73.  
  74. <sect2 id="realloc"><title>realloc</title>
  75. <para>
  76. <synopsis>
  77. void *realloc( void *current, <link linkend="size-t">size_t</link> size )</synopsis>
  78. Attempt to re-size a currently allocated region.
  79. </para>
  80. <sect3><title>Description</title>
  81. <para>
  82. realloc is used to resize a currently allocated block without loosing the 
  83. data contained within.  If the current block is larger than the requested 
  84. block the the trailing data is lost.  Note that the end of the block is
  85. not cleared if the current block is smaller than the requested one.
  86. If current is NULL, then this is equivalent to <link linkend="malloc">malloc</link>.
  87. If size is zero, then this is equivalent to <link linkend="free">free</link>.
  88. </para></sect3>
  89. <sect3><title>Parameters</title>
  90. <para>
  91. current: Pointer to the currently allocated block.
  92. </para>
  93. <para>
  94. size: Size of the new block
  95. </para></sect3>
  96. <sect3><title>Returns</title>
  97. <para>
  98. On success returns a pointer to the newly allocated block.  Note that the new
  99. block may be at a different location to the old.
  100. On failure or if size is zero, then NULL is returned.
  101. </para></sect3>
  102. </sect2>
  103. <sect2 id="free"><title>free</title>
  104. <para>
  105. <synopsis>int free( void *region )</synopsis>
  106. Free a previously allocated region.
  107. </para>
  108. <sect3><title>Description</title>
  109. <para>
  110. Attempts to free a region previously allocated by <link linkend="malloc">malloc</link>, 
  111. <link linkend="realloc">realloc</link> or <link linkend="calloc">calloc</link>.  Note that only valid regions can
  112. be freed.  Note also that this prototype differs from the standard C free
  113. as it returns an error code.
  114. </para></sect3>
  115. <sect3><title>Parameters</title>
  116. <para>
  117. region:  Pointer to a previously allocated region.
  118. </para></sect3>
  119. <sect3><title>Returns</title>
  120. <para>
  121. On success, returns zero (0).
  122. If the region is already free, returns -1.
  123. If the region was never allocated, returns -2.
  124. </para></sect3>
  125. </sect2></sect1>
  126. <sect1><title>Data types</title>
  127. <sect2 id="null"><title>NULL</title>
  128. <para>
  129. NULL is returned by <link linkend="malloc">malloc</link> and others upon failure.  Currently
  130. defined to be equal to zero.
  131. </para>
  132. </sect2>
  133. <sect2 id="size-t"><title>size_t:  UWORD</title>
  134. <para>
  135. size_t is used to specify the size of an object in bytes.  As the GB has an
  136. 8 bit processor with a 16 bit address space, size_t is currently defined as
  137. a UWORD.  Note that due to the banked nature of most GB programs, it might
  138. be changed to UDWORD at a later date.
  139. </para></sect2>
  140. </sect1>
  141. <sect1><title>Implementation</title>
  142. <para>
  143. This malloc library is implemented using a simple signally linked list of hunks
  144. where a hunk is a a header and section of memory that can be either free or used.
  145. There is nothing particularly clever about the allocation algorithms.  I'm
  146. an engineer as opposed to a computer scientist, so if the code gets the job
  147. done then it's close enough.  Note that I do not know how well this system will
  148. hold up against heavy fragmentation caused by allocating many small blocks
  149. and keeping some of them.  However, I also cant think of a program that you'd
  150. want to run on a GB that would do this.
  151. </para>
  152. <sect2><title>Functions</title>
  153. <sect3 id="malloc-init"><title>malloc_init</title>
  154. <para>
  155. <synopsis>int malloc_init( void )</synopsis>
  156. If the malloc system is currently uninitialised, initialise it.
  157. </para>
  158. <sect4><title>Description</title>
  159. <para>
  160. Checks to see if the header malloc_first is valid by checking its
  161. magic number.  If it is not, initialise it by marking it as free, setting it
  162. to occupy all of the free ram in the area C000h to D000h, setting the
  163. <link linkend="mmalloc-hunk">region header</link> pointer to NULL and finally setting the
  164. <link linkend="malloc-magic">magic number</link>.
  165. </para></sect4>
  166. <sect4><title>Parameters</title>
  167. <para>
  168. None
  169. </para></sect4>
  170. <sect4><title>Returns</title>
  171. <para>
  172. On success, returns zero (0).
  173. If the malloc system is already setup, return -1.
  174. </para></sect4>
  175. </sect3>
  176. <sect3 id="malloc-gc"><title>malloc_gc</title>
  177. <para>
  178. <synopsis>void malloc_gc( void )</synopsis>
  179. Perform garbage collection on the malloc hunk list by joining consecutive free
  180. blocks.
  181. </para>
  182. <sect4><title>Description</title>
  183. <para>
  184. malloc_gc is called by <link linkend="malloc">malloc</link> when an attempt to allocate a
  185. region fails.  malloc_gc attempts to improve the situation by scanning through
  186. the malloc hunk list and joining adjacent free blocks into one larger block.
  187. In each scan, if two adjacent free blocks are found then they are combined and
  188. the scan continued from the first block.  At the end of a scan, if any
  189. combinations were made then the list is rescanned.  I'm not really sure why the
  190. rescan is there as in theory all combinations should be made on the first pass.
  191. </para></sect4>
  192. <sect4><title>Parameters</title>
  193. <para>
  194. None
  195. </para></sect4>
  196. <sect4><title>Returns</title>
  197. <para>
  198. Nothing
  199. </para></sect4>
  200. </sect3>
  201. </sect2>
  202. <sect2><title>Data types</title>
  203. <sect3 id="mmalloc-hunk"><title>mmalloc_hunk</title>
  204. <para>
  205. <synopsis>
  206. struct smalloc_hunk {
  207.     UBYTE magic;
  208.     pmmalloc_hunk    next;
  209.     UWORD size;
  210.     int status;
  211. };
  212. </synopsis>
  213. mmalloc_hunk is an internal structure used by the <link linkend="malloc">malloc</link> library
  214. to keep track of allocated and free regions of memory.
  215. </para>
  216. <sect4><title>Members</title>
  217. <para>
  218. magic:  A magic number that identifies this as a valid mmalloc_hunk
  219. next:  Pointer to the next hunk, <link linkend="null">NULL</link> if this is the last.
  220. size:  Size in bytes of the hunk.
  221. status:  Current status of the block that this hunk refers to.  One of
  222. MALLOC_UNUSED (0), MALLOC_FREE (1) and MALLOC_USED (2).  I have no idea why
  223. I defined both a MALLOC_UNUSED and a MALLOC_FREE :)
  224. </para></sect4>
  225. </sect3>
  226. <sect3 id="malloc-magic"><title>MALLOC_MAGIC:  UBYTE</title>
  227. <para>
  228. The magic number associated with a valid malloc hunk header.  Currently set
  229. at a really boring 123.  Suggestions for something better will be greatly
  230. appreciated.
  231. </para>
  232. </sect3>
  233. </sect2>
  234. <sect2><title>Notes</title>
  235. <para>
  236. The <link linkend="mmalloc-hunk">header</link> for a hunk occurs just before the
  237. region.  Any errant programs that write past their region could overwrite
  238. this header and break the linked list.  But you get that.  Most routines
  239. check the magic number while walking the list and abort if a broken
  240. header is found.
  241. </para>
  242. <para>
  243. The amount of memory free after static variables are allocated is determined
  244. by using a new linker area called _HEAP, defined in crt0.s.  _HEAP occurs
  245. after _BSS, so it should occur at the start of free memory.  The only data
  246. initially in _HEAP is a reference to malloc_heap_start which is used
  247. by <link linkend="malloc-init">malloc_init</link>.  Note that it is possibly on a real GB for the
  248. magic number to occur in a bad place.  This should be fixed in crt0.s at
  249. the initialisation time.  malloc also shares the ram with the stack.
  250. Currently the problem of the stack growing down into allocated memory is
  251. lessened by allocating from low memory first and by providing a 512 byte
  252. buffer (set in <link linkend="malloc-init">malloc_init</link>).
  253. </para>
  254. <para>
  255. On cartridges with internal memory an extra 8k from A000h to BFFFh is
  256. available.  Unfortunately <link linkend="free">free</link> assumes that hunks are consecutive
  257. which causes problems.  Two solutions are shifting _BSS to start at A000h
  258. or defining an extra flag in <link linkend="mmalloc-hunk">the header</link> that is set if
  259. the next hunk is consecutive to the current one.  The second option would 
  260. also allow paged ram to be used, although it would have to be managed carefully.
  261. </para>
  262. </sect2>
  263. </sect1>
  264. </chapter>
  265. <chapter id="font-lib">
  266. <title>sys/font.h - Support for multiple fonts</title>
  267. <sect1><title>Functions</title>
  268. <sect2 id="init-font"><title>init_font</title>
  269. <para>
  270. <synopsis>
  271. void init_font(void)
  272. </synopsis>
  273. Initialise the font system by clearing all font handles and releasing all tiles.
  274. </para>
  275. <sect3><title>Description</title>
  276. <para>
  277. Initialises the font system.  This routine should be called at the start of the program before any calls to <link linkend="load-font">load_font</link>.
  278. </para></sect3>
  279. <sect3><title>Parameters</title>
  280. <para>
  281. None.
  282. </para></sect3>
  283. <sect3><title>Returns</title>
  284. <para>
  285. Nothing.
  286. </para></sect3>
  287. </sect2>
  288. <sect2 id="load-font"><title>load_font</title>
  289. <para>
  290. <synopsis>
  291. <link linkend="font-handle">FONT_HANDLE</link> load_font( void *<link linkend="font-structure">font_structure</link> )
  292. </synopsis>
  293. Attempt to load the font font, returning a <link linkend="font-handle">FONT_HANDLE</link> on success or NULL on failure.
  294. </para>
  295. <sect3><title>Description</title>
  296. <para>
  297. load_font should be called once for each font that is required.  Note that currently there is no unload_font support.
  298. </para></sect3>
  299. <sect3><title>Parameters</title>
  300. <para>
  301. font: pointer to the base of a valid font structure
  302. </para></sect3>
  303. <sect3><title>Returns</title>
  304. <para>
  305. On success, returns a <link linkend="font-handle">FONT_HANDLE</link>.  On failure, returns NULL (0)
  306. </para></sect3>
  307. </sect2>
  308. <sect2 id="set-font"><title>set_font</title>
  309. <para>
  310. <synopsis>
  311. void set_font( <link linkend="font-handle">FONT_HANDLE</link> font_handle )
  312. </synopsis>
  313. Set the current font to the previously loaded font font_handle
  314. </para>
  315. <sect3><title>Description</title>
  316. <para>
  317. set_font changes current output font to the one specified by font_handle
  318. </para></sect3>
  319. <sect3><title>Parameters</title>
  320. <para>
  321. font_handle: handle from a previous <link linkend="load-font">load_font</link> call.
  322. </para></sect3>
  323. <sect3><title>Returns</title>
  324. <para>
  325. Nothing.
  326. </para></sect3>
  327. <sect3><title>Bugs</title>
  328. <para>
  329. No check is made to see if font_handle is a valid font handle.
  330. </para></sect3>
  331. </sect2>
  332. <sect2 id="mprint-string"><title>mprint_string</title>
  333. <para>
  334. <synopsis>
  335. void mprint_string( char *string )
  336. </synopsis>
  337. Print string string using the current font.
  338. </para>
  339. <sect3><title>Description</title>
  340. <para>
  341. This is a temporary routine used to test the font library.
  342. </para></sect3>
  343. <sect3><title>Parameters</title>
  344. <para>
  345. string: null terminated string to print.
  346. </para></sect3>
  347. <sect3><title>Returns</title>
  348. <para>
  349. Nothing.
  350. </para></sect3>
  351. </sect2>
  352. </sect1>
  353. <sect1><title>Data types</title>
  354. <sect2 id="font-handle"><title>FONT_HANDLE:  UWORD</title>
  355. <sect3><title>Description</title>
  356. <para>
  357. A FONT_HANDLE is a 16 bit value returned from <link linkend="load-font">load_font</link> and used by <link linkend="set-font">set_font</link>.  Physically it is a pointer to an entry in the font_table.
  358. </para>
  359. </sect3>
  360. </sect2>
  361. <sect2 id="font-structure"><title>font_structure</title>
  362. <sect3><title>Description</title>
  363. <para>
  364. A font_structure is a container for the data related to a font, including the <link linkend="font-structure-encoding">encoding data</link> and <link linkend="font-structure-tile-data">tile</link> (bitmap) image data.
  365. Due to the variable length nature of the encoding table and the tile data no default C structure exists.
  366. </para></sect3>
  367. <sect3><title>Format</title>
  368. <para>
  369. A font structure is made up of four fields - the font type, the number of tiles used, the encoding table and the tile data.
  370. </para>
  371. <sect4 id="font-structure-type"><title>type:  UBYTE</title>
  372. <para>
  373. The Font type is a single bit that describes the encoding table and the format of
  374. the tile data.  The encoding table length is specified by the lower two bits
  375. 00:  256 byte encoding table
  376. 01:  128 byte encoding table
  377. 10 and 11 are reserved.
  378. The third bit (0x04) is used to determine if the tile data is compressed.  Many tiles do not use shades of grey, and so can be represented in 8 bytes instead of 16.  If bit 2 is set, then the tiles are assumed to be 8 bytes long and are expanded to 16 bytes at the load stage.
  379. </para></sect4>
  380. <sect4 id="font-structure-num-tiles"><title>num_tiles:  UBYTE</title>
  381. <para>
  382. num_tiles gives the number of tiles present in the tile data and hence the number of tiles required by the font.
  383. </para></sect4>
  384. <sect4 id="font-structure-encoding"><title>encoding:  array of UBYTE</title>
  385. <para>
  386. The encoding table is an array that maps an ASCII character to the appropriate tile in the tile data.  For example, suppose that the letter 'A' was the tenth tile.  Then the 65th (the ASCII code for 'A') entry in the encoding table would be 10.
  387. Any ASCII characters that don't have a corresponding tile should be mapped to a default tile.  Space is recommended tile.
  388. </para></sect4>
  389. <sect4 id="font-structure-tile-data"><title>tile_data:  array of UBYTE</title>
  390. <para>
  391. The final field is the actual tile data.  Note that tile numbering starts from zero.
  392. </para></sect4>
  393. </sect3>
  394. </sect2>
  395. </sect1>
  396. </chapter>
  397. </book>
  398.  
  399.